package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;

import com.example.musicplayer.DataLoadManager;
import com.example.musicplayer.R;
import com.example.musicplayer.settings.SettingsItem;
import com.example.musicplayer.view.SettingsView;
import com.example.musicplayer.windowTools.WindowTools;

import java.util.ArrayList;
import java.util.List;

public class BaseSettingsActivity extends BaseActivity {
    protected DataLoadManager mDataLoadManager;
    //check if user has login
    protected boolean mHasLoginCheck = true;
    private LinearLayout mSettingsContainer;
    private final List<SettingsView> mSettingsViews = new ArrayList<SettingsView>();

    private final SettingsView.SettingsClickListener mSettingsClickListener = this::onSettingsItemClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_settings);
        WindowTools.lightStatusBar(getWindow());
        getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        setEnableMusicTabView(true);
        //set toolbar
        Toolbar toolbar = findViewById(R.id.toolbar_base_settings);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        initView();
        //data load manager
        mDataLoadManager = DataLoadManager.getInstance();
        if (mHasLoginCheck && mDataLoadManager == null) {
            finish();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        for (int i = 0; i < mSettingsViews.size(); i++) {
            updateSettingsItem(mSettingsViews.get(i));
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        setPendingTransition(R.anim.normal_activity_enter, R.anim.slow_activity_exit);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setPendingTransition(R.anim.slow_activity_enter, R.anim.normal_activity_exit);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        //find view
        ((TextView) findViewById(R.id.settings_title)).setText(setActivityTitle());
        mSettingsContainer = findViewById(R.id.settings_container);
    }

    protected int setActivityTitle() {
        return R.string.description_null;
    }

    protected final void setActivityTitle(String title) {
        ((TextView) findViewById(R.id.settings_title)).setText(title);
    }

    @SuppressLint("InflateParams")
    protected View setSettingsDivideView() {
        View divideView = LayoutInflater.from(this).inflate(R.layout.base_settings_divide_view, null);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , (int) getResources().getDimension(R.dimen.base_settings_divide_height));
        divideView.setLayoutParams(lp);
        return divideView;
    }

    protected View setSettingsDivideView(String groupLabel) {
        return setSettingsDivideView();
    }

    protected void updateSettingsItem(SettingsView settingsView) {
    }

    protected LinearLayout getSettingsContainer() {
        return mSettingsContainer;
    }

    protected SettingsView.SettingsClickListener getSettingsClickListener() {
        return mSettingsClickListener;
    }

    protected SettingsView getSettingsView(int settingsItem) {
        for (int i = 0; i < mSettingsViews.size(); i++) {
            if (mSettingsViews.get(i).getSettingsItem() == settingsItem) {
                return mSettingsViews.get(i);
            }
        }
        return null;
    }

    protected void setSettingsItem(int settingsItem) {
        SettingsView view = SettingsItem.createSettingsItem(settingsItem);
        view.setClickListener(mSettingsClickListener);
        mSettingsContainer.addView(view.getSettingsView());
        mSettingsViews.add(view);
    }

    protected void setSettingsDivideItem() {
        mSettingsContainer.addView(setSettingsDivideView());
    }

    protected void setSettingsDivideItem(String groupLabel) {
        mSettingsContainer.addView(setSettingsDivideView(groupLabel));
    }

    protected void setSettingsEmptyItem(int height) {
        @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.empty_view, null);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , height);
        view.setLayoutParams(lp);
        mSettingsContainer.addView(view);
    }

    protected void onSettingsItemClick(SettingsView settingsView, boolean isChecked) {
    }

    protected void finishActivity() {
        finish();
        setPendingTransition(R.anim.slow_activity_enter, R.anim.normal_activity_exit);
    }
}